Core Java® Volume I—Fundamentals, Tenth Edition (Lloyd Laird's Library) by Cay S. Horstmann

Core Java® Volume I—Fundamentals, Tenth Edition (Lloyd Laird's Library) by Cay S. Horstmann

Author:Cay S. Horstmann
Language: eng
Format: epub
Publisher: Prentice Hall
Published: 2016-03-14T16:00:00+00:00


9.3.1 Basic Map Operations

The Java library supplies two general-purpose implementations for maps: HashMap and TreeMap. Both classes implement the Map interface.

A hash map hashes the keys, and a tree map uses an ordering on the keys to organize them in a search tree. The hash or comparison function is applied only to the keys. The values associated with the keys are not hashed or compared.

Should you choose a hash map or a tree map? As with sets, hashing is usually a bit faster, and it is the preferred choice if you don’t need to visit the keys in sorted order.

Here is how you set up a hash map for storing employees:

Click here to view code image

Map<String, Employee> staff = new HashMap<>(); // HashMap implements Map

Employee harry = new Employee("Harry Hacker");

staff.put("987-98-9996", harry);

...

Whenever you add an object to a map, you must supply a key as well. In our case, the key is a string, and the corresponding value is an Employee object.

To retrieve an object, you must use (and, therefore, remember) the key.

Click here to view code image

String id = "987-98-9996";

e = staff.get(id); // gets harry

If no information is stored in the map with the particular key specified, get returns null.

The null return value can be inconvenient. Sometimes, you have a good default that can be used for keys that are not present in the map. Then use the getOrDefault method.

Click here to view code image

Map<String, Integer> scores = ...;

int score = scores.get(id, 0); // Gets 0 if the id is not present

Keys must be unique. You cannot store two values with the same key. If you call the put method twice with the same key, the second value replaces the first one. In fact, put returns the previous value associated with its key parameter.

The remove method removes an element with a given key from the map. The size method returns the number of entries in the map.

The easiest way of iterating over the keys and values of a map is the forEach method. Provide a lambda expression that receives a key and a value. That expression is invoked for each map entry in turn.

Click here to view code image

scores.forEach((k, v) ->

System.out.println("key=" + k + ", value=" + v));

Listing 9.6 illustrates a map at work. We first add key/value pairs to a map. Then, we remove one key from the map, which removes its associated value as well. Next, we change the value that is associated with a key and call the get method to look up a value. Finally, we iterate through the entry set.

Listing 9.6 map/MapTest.java

Click here to view code image



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.